package com.tuo.service.impl;

import com.tuo.beans.*;
import com.tuo.service.*;
import com.tuo.util.BaseServiceImpl;
import com.tuo.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by tuo on 2016/1/11.
 */
@Service
public class RecordServiceImpl extends BaseServiceImpl<Record> implements RecordService {
    @Autowired
    DetailService detailService;
    @Autowired
    StoppedService stoppedService;
    @Autowired
    OrderService orderService;

    @Override
    public void task() throws Throwable{
        BigDecimal price = null;
        BigDecimal orderPrice = null;
        //计算所有order金额
        List<Orders> list = this.orderService.query(new Orders());
        for (Orders orders : list) {
            orderPrice = new BigDecimal(0.00);
            List<Detail> detaillist = this.detailService.query(new Detail(orders.getId()));
            for (Detail tail : detaillist) {
                if (tail.getNumber() == tail.getReturnNumber()) {
                    orderPrice = orderPrice.add(tail.getPrice());
                    continue;
                }
                price = new BigDecimal(0.00);
                List<Stopped> stoppeds = this.stoppedService.query(new Stopped(tail.getId()));
                for (Stopped stop : stoppeds) {
                    price = price.add(stop.getPrice());
                }
                int leave = tail.getNumber() - tail.getReturnNumber();
                long diffDay = DateUtil.string2DateDiffIncludeST(tail.getStart(), DateUtil.now());
                switch (tail.getAlgorithm()) {
                    case ONCE:
                        price = price.add(tail.getUnitPrice().multiply(new BigDecimal(leave)).setScale(2,BigDecimal.ROUND_HALF_UP));
                        break;
                    case FIXEDDAY:
                    case BYDAY:
                        price = price.add(new BigDecimal(diffDay).multiply(tail.getUnitPrice()).multiply(new BigDecimal(leave)).setScale(2,BigDecimal.ROUND_HALF_UP));
                        break;
                    case BYAREA:
                        price = tail.getUnitPrice().multiply(new BigDecimal(tail.getParams())).setScale(2,BigDecimal.ROUND_HALF_UP);
                        break;
                }
                tail.setPrice(price);
                this.detailService.update(tail);
                orderPrice = orderPrice.add(price);
            }
            orders.setPrice(orderPrice);
            {
                if (orders.getPaid() == null) {
                    orders.setPaid(new BigDecimal(0.00));
                }
                if (orders.getDiscount() == null) {
                    orders.setDiscount(new BigDecimal(0.00));
                }
                BigDecimal arrears = orderPrice.subtract(orders.getDiscount()).subtract(orders.getPaid()).subtract(orders.getAdvance());
                orders.setArrears(arrears);
            }
            this.orderService.update(orders);
        }
    }
}
